package framework
{
	import flash.utils.getDefinitionByName;
	import flash.utils.getQualifiedClassName;
	
	import mx.messaging.ChannelSet;
	import mx.messaging.Consumer;
	import mx.messaging.Producer;
	import mx.messaging.channels.AMFChannel;
	import mx.messaging.events.MessageEvent;
	import mx.messaging.messages.AsyncMessage;
	import mx.messaging.messages.IMessage;
	import mx.utils.UIDUtil;

	/**
	 * 订阅模式处理类
	 * @author Ken
	 */
	public class PollingBroker
	{
		/**本地发送出去的消息集合**/
		private static var messageArr:Array = [];
		
		/**订阅字符串频道**/
		private static var pollingCH:AMFChannel = new AMFChannel("pollingCH", "../messagebroker/amfpolling");
		
		/**订阅流频道**/
//		private static var streamingCH:StreamingAMFChannel = new StreamingAMFChannel("streamingCH", "../messagebroker/amfstreaming");
		
		/**消费**/
		private static var consumer:Consumer = new Consumer();
		
		/**发送**/
		private static var producer:Producer = new Producer();
		
		/**目标**/
		private static const DESTINATION:String = "polling-destination";
		
		/**轮询间隔**/
		private static var _pollingInterval:int = 0;
		public static function set pollingInterval(p_interval:int):void
		{
			_pollingInterval = p_interval;
			pollingCH.pollingInterval = _pollingInterval;
		}
		public static function get pollingInterval():int
		{
			return _pollingInterval;
		}
		
		/**
		 * 初始化轮询频道
		 */
		public static function init():void
		{
			//初始化频道信息
			pollingCH.pollingEnabled = true;
			
			//channel
			var cs:ChannelSet = new ChannelSet();
			cs.addChannel(pollingCH);
			
			//初始化consumer
			consumer.destination = DESTINATION;
			consumer.channelSet = cs;
			consumer.addEventListener(MessageEvent.MESSAGE, messageHandler);
			consumer.subscribe();
			
			//初始化producer
			producer.destination = DESTINATION;
			producer.channelSet = cs;
			producer.connect();
		}
		
		/**
		 * 接收到订阅信息回调函数
		 * @param p_evt
		 * @return
		 */
		private static function messageHandler(p_evt:MessageEvent):void
		{
			var imsg:IMessage = p_evt.message;
			var obj:Object = imsg.headers;
			var index:int = messageArr.indexOf(imsg.messageId);
			if(index >= 0){
				messageArr.splice(index, 1)
				//本地发出的消息，不作处理;
				if(!obj.localHandle)
					return;
			}
			var clz:Class = getDefinitionByName(obj.clz) as Class;
			var fun:Object = new clz;
			fun[obj.method](imsg.body, index >= 0 ? true : false);
		}
		
		/**
		 * 发送推送信息
		 * @param p_data
		 * @param p_method 回调函数名，回调函数格式function (result:Object, isSentBySelf:Boolean)
		 * @param p_class 回调函数所属类
		 * @param p_localHandle 本地是否处理自己发出的消息
		 * @return
		 */
		public static function sendPollingMsg(p_data:Object, p_method:String, p_class:Class, p_localHandle:Boolean = true):void
		{
			if(producer.connected){
				var msg:AsyncMessage = new AsyncMessage();
				msg.messageId = UIDUtil.createUID();
				msg.headers = {method:p_method, clz:getQualifiedClassName(p_class), localHandle:p_localHandle};
				msg.body = p_data;
				msg.timestamp = new Date().time;
				producer.send(msg);
				//加入本地发送消息队列
				messageArr.push(msg.messageId);
			}else{
				trace("订阅频道尚未初始化，请先调用init函数进行初始化！");
			}
		}
	}
}